import HttpHeaders from '@/core/http/constants/HttpHeaders'
import ApiConfigDto from '@/core/framework/config/types/model/ApiConfigDto'
import { AxiosRequestConfig } from 'axios'
import lodash from 'lodash'
import globalConfig from '@/core/framework/config/globalConfig'

class ServerApiUtils {
	addTokenHeader = (headers: any, token: string): any => {
		headers[HttpHeaders.AUTHORIZATION] = 'bearer ' + token
		return headers
	}
	addOnlyTokenHeader = (token: string): any => {
		return this.addTokenHeader({}, token)
	}

	convertApiConfig(apiConfig: ApiConfigDto): AxiosRequestConfig {
		return {
			url: this.getUriOrMockUri(apiConfig),
			method: apiConfig.method,
		} as AxiosRequestConfig
	}

	getUriOrMockUri(apiConfig: ApiConfigDto): string {
		if (lodash.isNil(apiConfig?.mock)) {
			if (lodash.isNil(globalConfig?.mock)) {
				return apiConfig.uri
			}
			if (globalConfig.mock) {
				return apiConfig.mockUri
			} else {
				return apiConfig.uri
			}
		} else {
			if (apiConfig.mock) {
				return apiConfig.mockUri
			} else {
				return apiConfig.uri
			}
		}
	}
}

export default new ServerApiUtils()
